/**
 * Copyright 2011 AJG van Schie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.googlecode.julius.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import julius.utilities.CollectionHelper;

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

public class DateHelper {

    // 2012-06-18T12:57:29.630356+00:00
    private static final DateTimeFormatter df = ISODateTimeFormat.dateTime();

    public static DateTime parse(final String val) {
        if (val.contains("CEST") || val.contains("CET")) {
            SimpleDateFormat dFormat = new SimpleDateFormat("MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
            Date dateAndTime;
            try {
                dateAndTime = dFormat.parse(val.substring(4));
                return new DateTime(dateAndTime.getTime());
            } catch (ParseException e) {
                throw new RuntimeException();
            }
        } else if (val.contains("T")) {
            return df.parseDateTime(val);
        } else {
            String c = val.replaceFirst(" ", "T");
            c = c + ".000000+00:00";
            return df.parseDateTime(c);
        }

    }

    public static LocalDate parseToDate(final String val) {
        return parse(val).toLocalDate();
    }

    public static List<LocalDate> getDatesBetween(final String from, final String to) {
        return getDatesBetween(ISODateTimeFormat.date().parseLocalDate(from), ISODateTimeFormat.date().parseLocalDate(to));
    }

    public static List<LocalDate> getDatesBetween(final LocalDate from, final LocalDate to) {
        List<LocalDate> days = CollectionHelper.list();
        LocalDate cursor = from;
        while (!cursor.isAfter(to)) {
            days.add(cursor);
            cursor = cursor.plusDays(1);
        }
        return days;
    }

    public static List<LocalDate> getWeekendsBetween(final LocalDate from, final LocalDate to) {
        List<LocalDate> weekends = CollectionHelper.list();
        for (LocalDate day : getDatesBetween(from, to)) {
            if (day.getDayOfWeek() == DateTimeConstants.SATURDAY || day.getDayOfWeek() == DateTimeConstants.SUNDAY) {
                weekends.add(day);
            }
        }
        return weekends;
    }

    public static List<LocalDate> getSprintDays(final LocalDate from, final LocalDate to) {
        List<LocalDate> weekends = getWeekendsBetween(from, to);
        List<LocalDate> all = getDatesBetween(from, to);
        return filter(all, weekends);
    }

    public static List<LocalDate> filter(final List<LocalDate> days, final List<LocalDate> skippableDays) {
        return CollectionHelper.relativeComplement(skippableDays, days);
    }

    public static String friendly(final LocalDate date) {
        DateTimeFormatter fmt = DateTimeFormat.forPattern("E d MMM");
        return fmt.print(date);

    }

    public static LocalDate parseToDateSimple(final String day) {
        return parseToDate(day + "T12:57:29.630356+00:00");
    }
}
